package com.home.task;

import com.home.base.util.FetchNum2Util;
import com.home.base.util.FetchNumUtil;
import com.home.base.util.FetchUtil;
import com.home.biz.ActivityBiz;
import com.home.biz.JobLogBiz;
import com.home.biz.PhoneBiz;
import com.home.biz.ProvBiz;
import com.home.config.InitDataConfig;
import com.home.entity.JobLog;
import com.home.entity.ProvInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/18 15:49
 * @description：
 * @modified By：
 * @version: $
 */
@Component
@Slf4j
public class FetchTask2 {
    @Autowired
    JobLogBiz logBiz;

    @Autowired
    ProvBiz provBiz;
    private String data="{\"MsgType\":\"LiveHKSelectNumberReq\",\"Version\":\"1.0.0\",\"provCode\":\"#PROV\",\"cityCode\":\"#CITY\",\"selecttype\":0,\"searchkey\":\"#KEY\",\"count\":10}";

    @Async
    public void activityTask(ActivityBiz activityBiz,String provName,String cityName,String activiName,String acitviCode,List<String> nums, String tNum){
        Instant begin = Instant.now();
        InitDataConfig.setFetchStatus(true);
        logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" 开始采集").build());
        try {

            List<Future<String>> resultList = new ArrayList<Future<String>>();
            ExecutorService executorService = Executors.newFixedThreadPool(Integer.valueOf(tNum));
            nums.stream().forEach(e->{

                Future<String > future = executorService.submit(new FetchNum2Util(activityBiz,provName,cityName,activiName,acitviCode,e,true));
                // 将任务执行结果存储到List中
                resultList.add(future);

            });
            for (Future<String> fs : resultList) {
                try {
                    log.debug("采集结果："+fs.get());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" 采集异常"+e.getMessage()).build());
            InitDataConfig.setFetchStatus(false);
            return;
        }
        Instant end = Instant.now();
        InitDataConfig.setFetchStatus(false);
        logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" 采集完成。耗时:"+ Duration.between(begin, end).toMinutes()+"分钟 采集共："+activityBiz.counts()+"个手机号码").build());
    }



    @Async
    public void activityTask(ActivityBiz activityBiz,String provName,String cityName,String activiName,String acitviCode,String sNum, String eNum, String tNum){
        Instant begin = Instant.now();
        InitDataConfig.setFetchStatus(true);
        logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" "+sNum+ "-"+eNum+"开始采集").build());
        try {

            List<Future<String>> resultList = new ArrayList<Future<String>>();
            ExecutorService executorService = Executors.newFixedThreadPool(Integer.valueOf(tNum));
            int fetchNum=Integer.valueOf(sNum);
            while (fetchNum<=Integer.valueOf(eNum)){
                //采集
                Future<String > future;
                if(acitviCode.equals("2001200012395003")){
                    future=executorService.submit(new FetchNumUtil(activityBiz,provName,cityName,activiName,acitviCode,String.format("%4d",fetchNum).replace(" ", "0"),true));

                }else {
                    future = executorService.submit(new FetchNumUtil(activityBiz,provName,cityName,activiName,acitviCode,String.format("%4d",fetchNum).replace(" ", "0")));

                }
               // 将任务执行结果存储到List中
                resultList.add(future);
                // phoneBiz.saveList(provInfo.getName(),parent.getName(),"移动花卡宝藏版", FetchUtil.sjData(data.replace("#PROV",provInfo.getProvcode().toString()).replace("#KEY",String.format("%4d",10).replace(" ", "0")).replace("#CITY",provInfo.getPid().toString())));

                fetchNum++;
            }
            for (Future<String> fs : resultList) {
                try {
                    log.debug("采集结果："+fs.get());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" "+sNum+ "-"+eNum+"采集异常"+e.getMessage()).build());
            InitDataConfig.setFetchStatus(false);
            return;
        }
        Instant end = Instant.now();
        InitDataConfig.setFetchStatus(false);
        logBiz.insertSelective(JobLog.builder().notes(activiName+" 线程数"+tNum+" "+sNum+ "-"+eNum+"采集完成。耗时:"+ Duration.between(begin, end).toMinutes()+"分钟 采集共："+activityBiz.counts()+"个手机号码").build());
    }
}
